﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 蓝牙注册工具.View
{
    /// <summary>
    /// ChannelView.xaml 的交互逻辑
    /// </summary>
    public partial class ChannelView : UserControl
    {
        public string Id { get; set; } = "1";

        public ViewModel.ChannelViewModel viewModel = new ViewModel.ChannelViewModel();
        public SerialPort serialPort;
        public BLL.ChannelTestBLL channelTestBLL;
        public bool isLoop = false;
        public string portName = "";
        public ChannelView()
        {
            InitializeComponent();
            DataContext = viewModel;
            channelTestBLL = new BLL.ChannelTestBLL(this);
            RichTextBoxLog.Document.Blocks.Remove(RichTextBoxLog.Document.Blocks.FirstBlock);
            
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                switch (int.Parse(Id))
                {
                    case 1: serialPort = BLL.PortBLL.serialPort1; serialPort.PortName = MainWindow.config.Channel1Portname; break;
                    case 2: serialPort = BLL.PortBLL.serialPort2; serialPort.PortName = MainWindow.config.Channel2Portname; break;
                    case 3: serialPort = BLL.PortBLL.serialPort3; serialPort.PortName = MainWindow.config.Channel3Portname; break;
                    case 4: serialPort = BLL.PortBLL.serialPort4; serialPort.PortName = MainWindow.config.Channel4Portname; break;

                    default:
                        break;
                }
                Loop();
            }
            catch (Exception ex)
            {
                BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.OrangeRed, ex.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //channelTestBLL.OpenPort();
            }
            catch (Exception ex)
            {
                BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.OrangeRed, ex.Message);
            }
        }
        public void Init()
        {
            if (state == 0)
            {
                viewModel.InfoColor = Brushes.LightGray;
                viewModel.MacText = "蓝牙地址";
                viewModel.VerText = "软件版本";
                viewModel.SearchColor = Brushes.LightGray;
                viewModel.DbmText = "信号强度";
                viewModel.UpColor = Brushes.LightGray;
                BtLabelBLL.UiBLL.RichTextBoxClearText(RichTextBoxLog);
                RichTextBoxLog.Document.Blocks.Remove(RichTextBoxLog.Document.Blocks.FirstBlock);
            }
        }
        int state = 0;
        private void Loop()
        {
            if (isLoop == true)
            {
                return;
            }
            Task.Run(async () =>
            {
                while (true)
                {
                    isLoop = true;
                    try
                    {
                        state = 0;
                        await Task.Delay(500);
                        Dispatcher.Invoke(() =>
                        {
                            switch (int.Parse(Id))
                            {
                                case 1: portName = MainWindow.config.Channel1Portname; break;
                                case 2: portName = MainWindow.config.Channel2Portname; break;
                                case 3: portName = MainWindow.config.Channel3Portname; break;
                                case 4: portName = MainWindow.config.Channel4Portname; break;
                                default:
                                    break;
                            }
                            viewModel.Port = portName;
                        });
                        lock (serialPort)
                        {
                            if (serialPort.IsOpen == false || portName != serialPort.PortName)
                            {
                                channelTestBLL.OpenPort(portName);
                            }
                        }
                        bool init = channelTestBLL.ReadInfo().Result;
                        if (init == false)
                        {
                            continue;
                        }
                        state = 1;
                        //await Task.Delay(1000);
                        bool dBm = await channelTestBLL.dBmCheck();
                        if (dBm == false)
                        {
                            BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.OrangeRed, "NG", 100);
                            continue;
                        }
                        bool up = await channelTestBLL.SignUp();

                        if (up == false)
                        {
                            BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.OrangeRed, "NG", 100);
                            continue;
                        }

                        lock (MainWindow.textBlockCount)
                        {
                            MainWindow.macDAL.Update(new BtLabelModel.MacDbModel.macs() { mac = viewModel.MacText, time = DateTime.Now });
                            MainWindow.textBlockCount.Dispatcher.Invoke(() =>
                            {
                                MainWindow.textBlockCount.Text = MainWindow.macDAL.GetCount().ToString();
                            });
                        }
                        BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.ForestGreen, "OK", 100);
                        //await Task.Delay(1000);
                    }
                    catch (Exception ex)
                    {
                        BtLabelBLL.UiBLL.RichTextBoxAppendText(RichTextBoxLog, Brushes.OrangeRed, ex.Message);
                    }
                }
            });
    }


    }
}
